from django.db import models
from django.db.models import *
from django.contrib import admin
from django.contrib.auth.models import User,UserManager
# Create your models here.

    
class Group(models.Model):
    leader = models.ForeignKey(User,unique=True)
    name = CharField(max_length = 30)
    introduction = CharField(max_length = 150)
    def quit(self,user):
        profiles = self.userprofile_set.all()
        if len(profiles) == 1:
            self.delete()          
        elif self.leader.username == user.username:
            self.leader = profiles[0].user
            self.save()
                    
        
class Homework(models.Model):
    Type =(
        (1 ,"Individual"),
        (2, "Team"),
        )
        
    title = CharField(max_length = 30)
    content = TextField()
    type = models.IntegerField(choices = Type,default = 1)
    publishTime = DateTimeField(default = datetime.datetime.now)
    deadLine = DateTimeField(default = datetime.datetime.now)
    
class StudentHomeworkManager(models.Manager):
    def create(self, u, h,g=-1):   
        sh = self.model(user=u,homework =h,grade=g)
        sh.save(using=self._db)
        return sh
    
class StudentHomework(models.Model):
    user = models.ForeignKey(User)
    homework = models.ForeignKey(Homework)
    objects = StudentHomeworkManager()
    #state
    grade = IntegerField(blank=True)
    
from UserProfile import *
    
    
    
    
    